home *** CD-ROM | disk | FTP | other *** search
/ Collection of Tools & Utilities / Collection of Tools and Utilities.iso / basic / ms2id_tb.zip / MSTOID.ASM next >
Assembly Source File  |  1987-10-06  |  5KB  |  132 lines

  1.         page    ,132
  2. ;
  3. ; MSTOID.ASM
  4. ;
  5. ; Turbo Basic $INLINE procedure for converting from
  6. ; Microsoft double precision format to Turbo's IEEE
  7. ; long real format
  8. ;
  9. ; declaration format:
  10. ;
  11. ;    SUB MSTOID INLINE
  12. ;          $INLINE "MSTOID.BIN"
  13. ;    END SUB
  14. ;
  15. ; calling example:
  16. ;
  17. ;    FIELD #1,8 AS MSNUMBER$            ' Microsoft format in file
  18. ;    MS# = CVD(MSNUMBER$)               ' do NOT use CVMD() here !!!
  19. ;    CALL MSTOID(MS#,I#)                ' converts to Turbo Basic format
  20. ;
  21. ;    where MS# is a double precision number in Microsoft floating
  22. ;    point format (get with 'CVD', not 'CVMD') and I# is a variable
  23. ;    which will receive the converted IEEE long real result.
  24. ;    You may use the same variable for MS# and I#.
  25. ;
  26. ; algorithm used:
  27. ;
  28. ;    1) Pointers to each variable are placed in DS:SI and ES:DI.  Each
  29. ;       variable is 8 bytes long and the bytes will be processed from
  30. ;       high addresses to low addresses (most significant bits first).
  31. ;
  32. ;    2) Considering each number as a 64 bit field (bit 63 = most
  33. ;       significant), the formats are
  34. ;
  35. ;       Microsoft:    Bits 63-56     = 8 bit exponent, biased 129
  36. ;                     Bit  55        = mantissa sign bit
  37. ;                     Bits 54-0      = 55 bit normalized mantissa
  38. ;
  39. ;       IEEE:         Bit  63        = mantissa sign bit
  40. ;                     Bits 62-52     = 11 bit exponent, biased 1023
  41. ;                     Bits 51-0      = 52 bit normalized mantissa
  42. ;
  43. ;    3) Zero is handled as a special case, in which all bits are
  44. ;       set to zero.  Although IEEE allows both positive and negative
  45. ;       zero, the Microsoft format uses only a positive zero.
  46. ;
  47. ;    4) The exponent is extracted from the Microsoft number and the
  48. ;       bias is adjusted to IEEE.  The Microsoft sign bit is placed
  49. ;       in front of the exponent and the combination is placed in BX.
  50. ;       This is now the first 12 bits (63-52).  To fill out 2 bytes,
  51. ;       the first four Microsoft mantissa bits are inserted, and BX
  52. ;       is stored as the first 16 IEEE bits.
  53. ;
  54. ;    5) There now remains 48 IEEE mantissa bits to be filled from the
  55. ;       most significant 48 Microsoft mantissa bits.  These are moved
  56. ;       in a loop which performs a 3 bit right shift.  The 3 least
  57. ;       significant Microsoft mantissa bits are lost.
  58. ;
  59. ;    6) The decimal precision of the Microsoft format is
  60. ;       log(2^56) = 16.9 digits, while the decimal precision of
  61. ;       the IEEE format is log(2^53) = 16.0 digits.  This loss of
  62. ;       precision is a trade-off for the extended exponent range
  63. ;       available with the IEEE format
  64. ;
  65.  
  66. MSBIAS  equ     129                     ; Microsoft exponent bias
  67. IBIAS   equ     1023                    ; IEEE exponent bias
  68.  
  69. code    segment
  70.         assume  cs:code
  71.  
  72.         org     0100h                   ; need .COM format for $INLINE
  73. start:
  74.         push    bp                      ; establish stack frame addressing
  75.         mov     bp,sp
  76.         push    ds                      ; save segment registers
  77.         push    es
  78.  
  79.         lds     si,[bp+10]              ; get pointer to MS# in DS:SI
  80.         les     di,[bp+6]               ; get pointer to I# ES:DI
  81.  
  82.         cmp     word ptr [si+6],0       ; check for input value of zero
  83.         jne     notzero                 ; no, need to convert
  84.         xor     al,al                   ; yes, zero out destination
  85.         mov     cx,8
  86.         cld
  87.         rep     stosb
  88.         jmp     done
  89.  
  90. notzero:
  91.         xor     ax,ax
  92.         mov     al,byte ptr [si+7]      ; get exponent byte
  93.         add     ax,(IBIAS - MSBIAS)     ; convert bias
  94.         shl     ax,1                    ; shift into position
  95.         shl     ax,1
  96.         shl     ax,1
  97.         shl     ax,1
  98.         mov     bh,byte ptr [si+6]      ; get byte with sign bit
  99.         and     bx,8000h                ; mask off sign bit
  100.         or      bx,ax                   ; add in exponent
  101.         mov     al,byte ptr [si+6]      ; get first 4 mantissa bits
  102.         ror     ax,1                    ; 3 lower bits to AH higher bits
  103.         ror     ax,1                    ; and first 4 mantissa bits
  104.         ror     ax,1                    ; to lower AL bits
  105.         and     al,0Fh                  ; mask off lower 4 bits
  106.         or      bl,al                   ; place with BX
  107.         mov     word ptr es:[di+6],bx   ; store exponent, sign, 4 mant. bits
  108.         mov     bx,6                    ; last byte moved
  109.  
  110. next:
  111.         dec     bx
  112.         ror     ax,1                    ; move upper 3 AH bits
  113.         ror     ax,1                    ; to lower 3 bits
  114.         ror     ax,1
  115.         ror     ax,1
  116.         ror     ax,1
  117.         mov     al,[bx+si]              ; fetch next source byte
  118.         ror     ax,1                    ; rotate 3 bits right
  119.         ror     ax,1                    ; to build byte of 3 + 5 bits
  120.         ror     ax,1
  121.         mov     es:[bx+di],al
  122.         cmp     bx,0                    ; are we done?
  123.         jg      next                    ; yes
  124.  
  125. done:
  126.         pop     es                      ; restore registers
  127.         pop     ds                      
  128.         pop     bp
  129.                                         ; Turbo handles the RET
  130. code    ends
  131.         end     start
  132.